<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model_Foto extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    public function getFoto($refMenu, $refContent="") {
        if ($refContent == '') {
            $sql="select * from data_foto where REF_MENU = ?";
            $query = $this->db->query($sql,array($refMenu));
        } else {
            $sql="select * from data_foto where REF_MENU = ? and REF_CONTENT = ?";
            $query = $this->db->query($sql,array($refMenu,$refContent));
        }

        return $query->result();
    }

    public function updatePathFoto($id, $pathName, $title) {
        if ($pathName == '') {
            $this->db->query("update data_foto set TITLE=? where id = ?", array($title,$id));
        } else {
            $this->db->query("update data_foto set PATH =?, TITLE=? where id = ?", array($pathName,$title,$id));
        }
    }

    public function hapusFoto($id) {
        $sql="select * from data_foto where ID = ?";
        $query = $this->db->query($sql,array($id));

        $result = $query->result();
        foreach($result as $row) {
            if (unlink(getcwd()."\\foto\\".$row->PATH)){
                $this->db->query("delete from data_foto where id = ?", array($id));
            } else {
                echo "Foto gagal dihapus";
            }
        }
    }

    public function tambahFoto($refMenu, $refContent, $title="") {
        $arr = array($title,'default.jpg',$refMenu,$refContent);
        $this->db->query("insert into data_foto(TITLE,PATH,REF_MENU,REF_CONTENT) values (?,?,?,?)", $arr);
    }
}
